package vutil

import (
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

func httpMock(url string, method string, body string, header http.Header) map[string]interface{} {
	httpClient := &http.Client{}
	request, err := http.NewRequest(strings.ToUpper(method), url, strings.NewReader(body))
	if err != nil {
		log.Println(err)
		return map[string]interface{}{
			"success": false,
			"code":    500,
			"msg":     err.Error(),
		}
	}
	request.Header = header
	response, err := httpClient.Do(request)
	if err != nil {
		log.Println(err)
		return map[string]interface{}{
			"success": false,
			"code":    500,
			"msg":     err.Error(),
		}
	}
	defer response.Body.Close() //ok, most of the time :-)
	bodyRes, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Println(err)
		return map[string]interface{}{
			"success": false,
			"code":    500,
			"msg":     err.Error(),
		}
	}
	return map[string]interface{}{
		"success": true,
		"code":    response.StatusCode,
		"data":    string(bodyRes),
	}
}